﻿using JXC.Core.Repository;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using JXC.Core.Models;

using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using JXC.Application.Dtos;
using JXC.EFCore;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 商品控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CommodityManagementController : ControllerBase
    {
        private readonly IRepository<CommodityInformationSheet, Guid> _repository;
        private readonly IMapper _mapper;
        private readonly AppDbContext _context;

        public CommodityManagementController(
            IRepository<CommodityInformationSheet, Guid> repository,
            IMapper mapper,
            AppDbContext context
            )
        {
            _repository = repository;
            _mapper = mapper;
            _context = context;
        }
        // GET: api/<CommodityManagementController>
        [HttpGet]
        public async Task<IActionResult> Get()
        {
            List<CommodityInformationSheet> CommodityInformationSheetList = await _context.CommodityInformationSheet.Include(p => p.GoodsType).Include(p=>p.Cinfo) .ToListAsync();
            List<ShopDto> shopList = _mapper.Map<List<ShopDto>>(CommodityInformationSheetList);
            return Ok(shopList);
        }




        // POST api/<CommodityManagementController>
        [HttpPost]
        public async Task<IActionResult> Post([FromBody] ShopDto shopDto)
        {
            CommodityInformationSheet commodityInformationSheet = _mapper.Map<CommodityInformationSheet>(shopDto);
            CommodityInformationSheet res = await _repository.InsertAsync(commodityInformationSheet);
            if (res == null)
            {
                return BadRequest("添加失败了");
            }
            else
            {
                return NoContent();
            }
        }

        // PUT api/<CommodityManagementController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<CommodityManagementController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
